import numpy as np
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler

# 1. 加载示例数据（以 Iris 数据集为例）
data = load_iris()
X = data.data  # 特征数据
# print(X)
y = data.target  # 类别标签
print(y)

# 2. 标准化数据
X = StandardScaler().fit_transform(X)

# 3. 使用 t-SNE 降维
tsne = TSNE(n_components=2, random_state=42)
X_tsne = tsne.fit_transform(X)

# 4. 可视化结果
plt.figure(figsize=(8, 6))
scatter = plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=y, cmap='viridis')

# 添加标签
# plt.colorbar(scatter, label='Target Class')
plt.title('t-SNE visualization of Iris dataset')
plt.xlabel('t-SNE Component 1')
plt.ylabel('t-SNE Component 2')

# 显示图像
plt.show()
